#include "lame.h"
#include "shared_ptr.hpp"

#include "Common.h"
#include "WavFile.h"
#include "ListenThread.h"
#include "WorkThread.h"
#include "TTSCaller.h"
#include "FileDescriptor.h"

#include <cstdio>
#include <cstdlib>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <vector>

using namespace std;

const uint32 WORKER_CNT = 10;

//int main()
//{
//    string text = "Hello world, Baidu company";
//    LAN lan = EN;
//    ENCODING encoding = GBK;
//
//    TTSCaller ttsCaller;
//    ttsCaller.SetTTSReq(text, lan, encoding);
//
//    FileDescriptor sockfd(socket(AF_INET, SOCK_STREAM, 0));
//    boost::shared_ptr<Item> wav(new Item());
//    ttsCaller.TalkWithTTS(wav.get());
//
//    cout << "Wav file size = " << wav.get()->_dataLen << endl;
//
//    FileDescriptor filefd(open("jtts.wav", O_CREAT | O_WRONLY));
//    if (filefd.FD() < 0)
//    {
//        cout << "Failed to open jtts.wav" << endl;
//        return 0;
//    }
//    writen(filefd.FD(), wav.get()->_data, wav.get()->_dataLen);
//}

int main()
{
    ListenThread* listener = new ListenThread(4040);
    vector<WorkThread*> workers;

    for (int i = 0; i < WORKER_CNT; i++)
    {
        workers.push_back(new WorkThread());
    }

    listener->Run();
    cout << "Listen thread started: " << listener->GetThreadId() << endl;

    for (vector<WorkThread*>::iterator it = workers.begin();
        it != workers.end(); it++)
    {
        (*it)->Run();
        cout << "Work thread started: " << (*it)->GetThreadId() <<endl;
    }

    listener->Join();
    for (vector<WorkThread*>::iterator it = workers.begin();
            it != workers.end(); it++)
    {
        (*it)->Join();
    }
}

